<?php

namespace common\service;

use common\model\StepModel;
use common\model\StepProjectModel;
use support\SingletonTrait;

class StepProjectService
{

    // 单例工具
    use SingletonTrait;

    /**
     * 设置工序上下游组装限制配置
     * @param $stepId
     * @param $projectId
     * @param $isSkipUpstream
     * @return array|bool
     * @throws \Exception
     */
    public function setStepSkipUpstream($stepId, $projectId, $isSkipUpstream)
    {
        $stepProjectModel = model(StepProjectModel::class);
        $item = $stepProjectModel->where(['step_id' => $stepId, 'project_id' => $projectId])->find();
        $data['step_id'] = $stepId;
        $data['project_id'] = $projectId;
        $data['is_skip_upstream'] = $isSkipUpstream;
        if (!empty($item)) {
            // 修改配置
            $data['id'] = $item['id'];
            return $stepProjectModel->modifyItem($data);
        }
        // 增加配置
        return $stepProjectModel->addItem($data);
    }

    /**
     * 获取项目工序组装限制状态
     * @param $param
     * @param $projectId
     * @return array
     */
    public function getSkipUpstreamStatus($param, $projectId)
    {
        $fields = $param['fields'] ?? '';
        $filter = $param['filter'] ?? [];
        $page = $param['page'] ?? [0, C("database.database_max_select_rows")];
        $stepList = model(StepModel::class)
            ->field($fields)
            ->where($filter)
            ->page($page[0], $page[1])
            ->select();
        if (empty($stepList)) {
            return [];
        }

        $stepIds = array_column($stepList, 'id');
        $stepProjectList = model(StepProjectModel::class)->where([
            'step_id' => ['IN', join(',', $stepIds)],
            'project_id' => $projectId
        ])->select();

        $stepIdMap = array_column($stepProjectList, null, 'step_id');
        return array_map(function ($tmp) use ($stepIdMap) {
            $isSkipUpstreamArr = $stepIdMap[$tmp['id']] ?? [];
            $tmp['is_skip_upstream'] = $isSkipUpstreamArr['is_skip_upstream'] ?? 'no';
            $tmp['is_manual_submit'] = $isSkipUpstreamArr['is_manual_submit'] ?? 'no';
            $tmp['is_validate'] = $isSkipUpstreamArr['is_validate'] ?? 'no';
            $tmp['is_repair_restricted'] = $isSkipUpstreamArr['is_repair_restricted'] ?? 'no';
            $tmp['is_compress_required'] = $isSkipUpstreamArr['is_compress_required'] ?? 'yes';
            $tmp['is_modified_state'] = $isSkipUpstreamArr['is_modified_state'] ?? 'no';

            return $tmp;
        }, $stepList);
    }

    /**
     * 获取是否跳过上下游组装
     * @param $stepId
     * @param $projectId
     * @return bool
     */
    public function getIsSkipUpstream($stepId, $projectId)
    {
        $stepProjectData = model(StepProjectModel::class)
            ->field('id,step_id,is_skip_upstream')
            ->where([
                'step_id' => $stepId,
                'project_id' => $projectId,
                'is_skip_upstream' => 'yes'
            ])->find();
        if (empty($stepProjectData)) {
            return true;
        }
        return false;
    }

    /**
     * 设置配置
     * @param int $stepId
     * @param int $projectId
     * @param array $data
     * @return array|bool
     * @throws \Exception
     */
    public function setData(int $stepId, int $projectId, array $data)
    {
        $stepProjectModel = model(StepProjectModel::class);
        $item = $stepProjectModel->where(['step_id' => $stepId, 'project_id' => $projectId])->find();
        if (!empty($item)) {
            // 修改配置
            $data['id'] = $item['id'];
            return $stepProjectModel->modifyItem($data);
        }
        // 增加配置
        return $stepProjectModel->addItem($data);
    }

    /**
     * 查询配置
     * @param $stepId
     * @param $projectId
     * @return array
     */
    public function getData($stepId, $projectId)
    {
        $stepProjectModel = model(StepProjectModel::class);
        $item = $stepProjectModel->where(['step_id' => $stepId, 'project_id' => $projectId])->find();
        $res = [];
        $res['is_skip_upstream'] = $item['is_skip_upstream'] ?? 'no';
        $res['is_manual_submit'] = $item['is_manual_submit'] ?? 'no';
        $res['is_modified_state'] = $item['is_modified_state'] ?? 'no';
        return $res;

    }
}